﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Configuration;
using System.Web.Script.Services;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace freecart
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://code.google.com/p/freecart/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]

    public class WebService : System.Web.Services.WebService
    {
        private Freecart cart = new Freecart();
        
        [WebMethod(Description = "Adds an item to shopping cart", EnableSession = true)]
        public int CountCartItems()
        {
            if (CartManager.Carts == null || Context.Session["SessionId"] == null)
            {
                return 0;
            } 
            else {
                return CartManager.Carts.Select("SessionId = '" + Context.Session["SessionId"] + "'").Length;
            }
        }

        [WebMethod(Description = "Adds item to shopping cart", EnableSession = true)]
        public bool AddItem(string jsonForm)
        {
            if (CartManager.Carts == null)
            {
                // no nothing
            }
            else
            {
                if (Context.Session["SessionId"] == null)
                {
                    Context.Session["SessionId"] = System.Guid.NewGuid();
                }

                cart.AddItemToCart(Context.Session["SessionId"].ToString(), jsonForm);
            }

            return false;
        }

        [WebMethod(Description = "Removes item from shopping cart", EnableSession = true)]
        public bool RemoveItem(string ItemId)
        {
            if (Context.Session["SessionId"] == null || CartManager.Carts == null)
            {
                throw new ArgumentException("Cart session, or table, not found");
            }
            else
            {
                if (!String.IsNullOrEmpty(ItemId))
                {
                    string[] toRemove = ItemId.Split(',');
                    if (toRemove.Length >= 0)
                    {
                        bool response = true;
                        foreach (string removeItemId in toRemove)
                        {
                            if (cart.RemoveItemFromCart((Guid)Context.Session["SessionId"], removeItemId) == false)
                            {
                                response = false;
                            }
                        }
                        return response;
                    }
                    else {
                        return false;
                    }
                }
                return false;
            }
        }

        [WebMethod(Description = "Updates item quantity amount in shopping cart", EnableSession = true)]
        public bool UpdateQuantity(string ItemId, string Quantity)
        {
            if (Context.Session["SessionId"] == null || CartManager.Carts == null)
            {
                throw new ArgumentException("Cart session, or table, not found");
            }
            else
            {
                if (Quantity == "0")
                {   // remove if quantity is zero
                    return this.RemoveItem(ItemId);
                }
                else
                {
                    return cart.UpdateQuantity((Guid)Context.Session["SessionId"], ItemId, Quantity);
                }
            }                
        }
    }
}
